package com.kvest.NewLocationTest.service;

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
import com.kvest.NewLocationTest.LoggerService;

import java.util.List;

public class LocationService extends IntentService {
    public static final String ACTION_LOCATION = "com.kvest.ACTION_LOCATION";
    public static final String ACTION_ACTION = "com.kvest.ACTION_ACTION";
    public static final String ACTION_GEOFENCE = "com.kvest.ACTION_GEOFENCE";

    public LocationService() {
        super("LocationService");
    }

//    @Override
//    public void onCreate() {
//        super.onCreate();
//        LoggerService.getInstance().writeLog("onCreate LocationService");
//    }
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        LoggerService.getInstance().writeLog("onDestroy LocationService");
//    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getAction();

        if (ACTION_LOCATION.equals(action)) {
            Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
            LoggerService.getInstance().writeLog("location from service=" + location.toString());
        } else if (ACTION_ACTION.equals(action)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            List<DetectedActivity> detectedActivitys = result.getProbableActivities();
            if (!detectedActivitys.isEmpty()) {
                DetectedActivity activity = detectedActivitys.get(0);
                switch (activity.getType()) {
                    case DetectedActivity.IN_VEHICLE: setDetectedActivity("IN_VEHICLE"); break;
                    case DetectedActivity.ON_BICYCLE: setDetectedActivity("ON_BICYCLE"); break;
                    case DetectedActivity.ON_FOOT: setDetectedActivity("ON_FOOT"); break;
                    case DetectedActivity.STILL: setDetectedActivity("STILL"); break;
                    case DetectedActivity.TILTING: setDetectedActivity("TILTING"); break;
                    case DetectedActivity.UNKNOWN: setDetectedActivity("UNKNOWN"); break;
                }
            }
        } else if (ACTION_GEOFENCE.equals(action)) {
            int transition = LocationClient.getGeofenceTransition(intent);
            List<Geofence> crossedGeofences = LocationClient.getTriggeringGeofences(intent);
            if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                LoggerService.getInstance().writeLog("ENTER TO " + crossedGeofences.toString());
            } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
                LoggerService.getInstance().writeLog("EXIT FROM " + crossedGeofences.toString());
            }
        }
    }

    private void setDetectedActivity(String value){
        LoggerService.getInstance().writeLog(value);
    }
}
